#include "widget.h"
#include "ui_widget.h"
#include"QDebug"
#include "QValidator"
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->lineEdit->setPlaceholderText("请输入姓名");
    ui->lineEdit->setEchoMode(QLineEdit::Normal);
    ui->lineEdit->setFrame(true);
    ui->lineEdit->setClearButtonEnabled(true);
    ui->lineEdit_2->setPlaceholderText("请输入密码");
    ui->lineEdit_2->setEchoMode(QLineEdit::Password);
    ui->lineEdit_2->setFrame(true);
    ui->lineEdit_2->setClearButtonEnabled(true);
    ui->lineEdit_3->setPlaceholderText("请确认密码");
    ui->lineEdit_3->setEchoMode(QLineEdit::Password);
    ui->lineEdit_3->setFrame(true);
    ui->lineEdit_3->setClearButtonEnabled(true);
    ui->lineEdit_4->setPlaceholderText("请输入电话号码");
    ui->lineEdit_4->setEchoMode(QLineEdit::Normal);
    ui->lineEdit_4->setFrame(true);
    ui->lineEdit_4->setClearButtonEnabled(true);
    //ui->lineEdit_4->setInputMask("000-0000-0000");
    ui->pushButton->setEnabled(false);//禁用按钮
    ui->lineEdit_4->setValidator(new QRegExpValidator(QRegExp("^1[3-9]\\d{9}$")));//设置首位1，第二位2-9，后面9位
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_clicked()
{
    QString gender = ui->radioButton->isChecked() ? "男":"女";
    qDebug() << "姓名: " << ui->lineEdit->text()
    << "密码: " << ui->lineEdit_2->text()
    << "性别: " << gender
    << "⼿机: " << ui->lineEdit_4->text();

}

void Widget::on_lineEdit_2_textEdited(const QString &arg1)
{ (void)arg1;
   passwordjudge();


}

void Widget::on_lineEdit_3_textEdited(const QString &arg1)
{
    (void)arg1;
       passwordjudge();


}
void Widget::passwordjudge()//封装起来，降低耦合性
{
    const QString & s1=ui->lineEdit_2->text();//获取密码
     const QString & s2=ui->lineEdit_3->text();
    if(s1.isEmpty()&&s2.isEmpty())//判断两个密码状态
    {
        ui->label_word->setText("密码为空");

    }
    else if(s1!=s2)
    {
        ui->label_word->setText("密码不同");

    }
    else{
       ui->label_word->setText("密码一致");
    }
}

void Widget::on_checkBox_toggled(bool checked)//更改状态触发
{
    if(checked)
      {
        ui->lineEdit_2->setEchoMode(QLineEdit::Normal);
    }
    else
    {
         ui->lineEdit_2->setEchoMode(QLineEdit::Password);
    }
}

void Widget::on_lineEdit_4_textEdited(const QString &arg1)
{
    int pos=0;
    QString content=arg1;//把const变量变成普通变量
    if(ui->lineEdit_4->validator()->validate(content,pos)==QValidator::Acceptable)
    {
        ui->pushButton->setEnabled(true);

    }
    else
    {
        ui->pushButton->setEnabled(false);
    }

}
